home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / htmlHomePageWins.tcl < prev    next >
Encoding:
Text File  |  2001-01-13  |  10.6 KB  |  342 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  HTML mode - tools for editing HTML documents
  4.  # 
  5.  #  FILE: "htmlHomePageWins.tcl"
  6.  #                                    created: 99-07-20 18.03.16 
  7.  #                                last update: 01-01-13 20.33.39 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 3.0
  13.  # 
  14.  # Copyright 1996-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This file contains procs for the Home Page Windows submenu.
  35. #===============================================================================
  36.  
  37. #===============================================================================
  38. # ◊◊◊◊ Home page windows ◊◊◊◊ #
  39. #===============================================================================
  40.  
  41. proc html::OpenHPwin {{folder ""}} {
  42.     global html::HomePageWins
  43.     # Get folder to open.
  44.     if {$folder == "" && [catch {html::GetDir "Open:"} folder]} {return}
  45.     set tail [file tail $folder]
  46.     # Is their already a window for this folder?
  47.     if {[info exists html::HomePageWins($folder)]} {
  48.         bringToFront [set html::HomePageWins($folder)]
  49.         return    
  50.     }
  51.     set fileList [glob -nocomplain -dir $folder *]
  52.     
  53.     set text "$folder\rcmd-shift-C to copy URL\r"
  54.     foreach fil $fileList {
  55.         append text [file tail $fil] \r
  56.     }
  57.     if {[set winsize [html::GetHPwinSize $folder]] == ""} {
  58.         new -n $tail -m Home
  59.     } else {
  60.         eval new -n [list "$tail"] -g $winsize -m Home
  61.     }
  62.     insertText $text
  63.     if {$winsize == ""} {shrinkWindow 1}
  64.     # make folders boldface
  65.     for {set i 0} {$i < [llength $fileList]} {incr i} {
  66.         set fil [lindex $fileList $i]
  67.         if {[file isdirectory $fil]} {
  68.             insertColorEscape [rowColToPos [expr {$i + 3}] 0] bold
  69.             insertColorEscape [rowColToPos [expr {$i + 4}] 0] 12
  70.         }
  71.     }
  72.     html::SetWin
  73.     set html::HomePageWins($folder) [lindex [winNames] 0]
  74. }
  75.  
  76. # Reads a saved home page window size.
  77. proc html::GetHPwinSize {folder} {
  78.     global html::PrefsFolder html::HPwinPositions
  79.     if {[info exists html::HPwinPositions($folder)]} {return [set html::HPwinPositions($folder)]}
  80.     if {![file exists [file join ${html::PrefsFolder} "Home page window positions"]]} {return}
  81.     set cid [scancontext create]
  82.     set pos ""
  83.     scanmatch $cid "^\{?$folder\[ \}\]" {
  84.         if {[lindex $matchInfo(line) 0] == $folder} {set pos [lrange $matchInfo(line) 1 end]}
  85.     }
  86.     set fid [open [file join ${html::PrefsFolder} "Home page window positions"]]
  87.     scanfile $cid $fid
  88.     close $fid
  89.     scancontext delete $cid
  90.     return $pos
  91. }
  92.  
  93. proc html::QuitHook {} {
  94.     global html::PrefsFolder html::HPwinPositions
  95.     if {![info exists html::HPwinPositions]} {return}
  96.     message "Saving home page window positions…"
  97.     set current ""
  98.     if {[file exists [file join ${html::PrefsFolder} "Home page window positions"]] && 
  99.     ![catch {open [file join ${html::PrefsFolder} "Home page window positions"]} fid]} {
  100.         set current [split [read -nonewline $fid] \n]
  101.         close $fid
  102.     }
  103.     foreach c $current {
  104.         if {[info exists html::HPwinPositions([lindex $c 0])]} {
  105.             append n [lrange $c 0 0] " " [set html::HPwinPositions([lindex $c 0])] \n
  106.             unset html::HPwinPositions([lindex $c 0])
  107.         } else {
  108.             append n $c \n
  109.         }
  110.     }
  111.     foreach c [array names html::HPwinPositions] {
  112.         append n [list $c] " " [set html::HPwinPositions($c)] \n
  113.     }
  114.     if {![catch {open [file join ${html::PrefsFolder} "Home page window positions"] w} fid]} {
  115.         puts -nonewline $fid $n
  116.         close $fid
  117.     }
  118. }
  119.  
  120.  
  121. # Quick search in home page windows just like in Finder windows.
  122. proc html::SearchInHPwin {char} {
  123.     global Home::Time Home::hpWinString
  124.     set t [ticks]
  125.     if {[expr {$t - ${Home::Time}}] > 60} {set Home::hpWinString ""}
  126.     append Home::hpWinString $char
  127.     set Home::Time $t
  128.     if {[catch {search -s -f 1 -m 0 -r 1 -i 1 "^${Home::hpWinString}" [nextLineStart [nextLineStart [minPos]]]} res]} {return}
  129.     select [lindex $res 0] [nextLineStart [lindex $res 1]]
  130. }
  131.  
  132. proc html::HomeReturn {{opt 0}} {
  133.     global HTMLmodeVars
  134.     set f [html::GetAhpLine]
  135.     if {![file exists $f]} {alertnote "[file tail $f] not found."; return}
  136.     if {[file isdirectory $f]} {
  137.         if {$opt} {killWindow}
  138.         html::OpenHPwin $f
  139.     } else {
  140.         getFileInfo $f a
  141.         if {$a(type) == "TEXT"} {
  142.             if {$opt} {killWindow}
  143.             edit -c $f
  144.         } elseif {$HTMLmodeVars(homeOpenNonTextFile)} {
  145.             if {$a(type) == "APPL"} {
  146.                 if {$opt} {killWindow}
  147.                 launch -f $f
  148.             } elseif {$a(creator) == "MACS"} {
  149.                 beep; message "Cannot open."
  150.             } else {
  151.                 if {$opt} {killWindow}
  152.                 launchDoc $f
  153.             }
  154.         } else {
  155.             beep; message "Not a text file."
  156.         }
  157.     }
  158. }
  159.  
  160. proc html::HpWinBack {{opt 0}} {
  161.     set folder [file dirname [html::GetHpFolder]]
  162.     if {$folder != ""} {
  163.         if {$opt} {killWindow}
  164.         html::OpenHPwin $folder
  165.     }
  166. }
  167.  
  168. proc html::GetAhpLine {} {
  169.     return [file join [html::GetHpFolder] \
  170.       [getText [lineStart [getPos]] [pos::math [nextLineStart [getPos]] - 1]]]
  171. }
  172.  
  173. proc html::GetHpFolder {} {
  174.     return [getText [minPos] [pos::math [nextLineStart [minPos]] - 1]]
  175. }
  176.  
  177. # Refreshes a Home page window.
  178. proc html::RefreshHpWin {} {
  179.     set curSel [file tail [html::GetAhpLine]]
  180.     set folder [html::GetHpFolder]
  181.     setWinInfo read-only 0
  182.     if {![file exists [string trimright [file join ${folder} " "]]]} {killWindow; return}
  183.     set files [glob -nocomplain -dir $folder *]
  184.     set len [llength $files]
  185.     set pos [nextLineStart [nextLineStart [minPos]]]
  186.     set ind 0
  187.     while {$pos < [maxPos] && $ind < $len} {
  188.         set f [file tail [lindex $files $ind]]
  189.         set t [string trim [getText $pos [nextLineStart $pos]]]
  190.         while {$pos < [maxPos] && $ind < $len && $t == $f} {
  191.             incr ind
  192.             set pos [nextLineStart $pos]
  193.             set f [file tail [lindex $files $ind]]
  194.             set t [string trim [getText $pos [nextLineStart $pos]]]
  195.         }
  196.         if {[string compare [string tolower $t] [string tolower $f]] == 1} {
  197.             goto $pos
  198.             insertText $f \r
  199.             if {[file isdirectory [lindex $files $ind]]} {
  200.                 insertColorEscape $pos bold
  201.                 if {![file isdirectory [lindex $files [expr {$ind + 1}]]]} {
  202.                     insertColorEscape [nextLineStart $pos] 12
  203.                 }
  204.             } elseif {[file isdirectory [lindex $files [expr {$ind + 1}]]]} {
  205.                 insertColorEscape $pos 12
  206.                 insertColorEscape [nextLineStart $pos] bold
  207.             }            
  208.             set pos [nextLineStart $pos]
  209.             incr ind
  210.         } else {
  211.             deleteText $pos [nextLineStart $pos]
  212.         }
  213.         if {$pos < [maxPos]} {set t [string trim [getText $pos [nextLineStart $pos]]]}
  214.         set f [file tail [lindex $files $ind]]
  215.     }
  216.     if {$pos < [maxPos]} {
  217.         deleteText [pos::math $pos - 1] [maxPos]
  218.     } else {
  219.         goto [maxPos]
  220.         insertColorEscape $pos 12
  221.         foreach f [lrange $files $ind end] {
  222.             insertText [file tail $f] \r
  223.             if {[file isdirectory $f]} {
  224.                 insertColorEscape $pos bold
  225.                 insertColorEscape [nextLineStart $pos] 12
  226.             }
  227.             set pos [nextLineStart $pos]    
  228.         }
  229.     }
  230.     refresh
  231.     setWinInfo dirty 0
  232.     setWinInfo read-only 1
  233.     beginningOfBuffer
  234.     if {![catch {search -s -f 1 -m 0 -r 1 -- "^$curSel" [minPos]} res]} {
  235.         select [lindex $res 0] [nextLineStart [lindex $res 1]]
  236.     }
  237. }
  238.  
  239. proc html::RefreshWindows {} {
  240.     global html::HomePageWins
  241.     set frontWin [lindex [winNames -f] 0]
  242.     foreach folder [array names html::HomePageWins] {
  243.         bringToFront [set html::HomePageWins($folder)]
  244.         html::RefreshHpWin
  245.     }
  246.     bringToFront $frontWin
  247. }
  248.  
  249. # Copies an URL from a home page window.
  250. proc html::CopyURL {} {
  251.     global html::HomePageWinURL
  252.     set html::HomePageWinURL [html::GetAhpLine]
  253.     message "${html::HomePageWinURL} copied."
  254. }
  255.  
  256. # Pastes a previously copied URL from a home page window.
  257. proc html::PasteURL {} {
  258.     global html::HomePageWinURL htmlIsSel htmlCurSel HTMLmodeVars html::WrapPos html::AbsPos
  259.     if {![info exists html::HomePageWinURL]} {message "No URL to paste."; return}
  260.     if {[set link [html::GetFile 0 ${html::HomePageWinURL} 2]] == ""} {return}
  261.     set url [html::URLescape2 [lindex $link 0]]
  262.     html::GetSel
  263.     set html::AbsPos [getPos]
  264.     set html::WrapPos [posX [getPos]]
  265.     if {[llength [set wh [lindex $link 1]]]} {
  266.         set text [html::SetCase <IMG]
  267.         append text [html::WrapTag "[html::SetCase SRC=]\"$url\""]
  268.         append text [html::WrapTag [html::SetCase "WIDTH=\"[lindex $wh 0]\""]]
  269.         append text [html::WrapTag [html::SetCase "HEIGHT=\"[lindex $wh 1]\">"]]
  270.         set closing ""
  271.     } else {
  272.         set text "<[html::SetCase A]"
  273.         append text [html::WrapTag [html::SetCase HREF=]\"$url\">]
  274.         if {!$htmlIsSel} {append text "•content•"}
  275.         set closing [html::CloseElem A]
  276.         if {!$htmlIsSel && $HTMLmodeVars(useTabMarks)} {append closing "•end•"}
  277.     }
  278.     append text $htmlCurSel
  279.     append text $closing
  280.     if {$htmlIsSel} { deleteSelection }
  281.     elec::Insertion $text
  282. }
  283.  
  284.  
  285. # closeHook
  286. proc html::CloseHook {name} {
  287.     global html::HomePageWins
  288.     foreach folder [array names html::HomePageWins] {
  289.         if {$name == [set html::HomePageWins($folder)]} {
  290.             unset html::HomePageWins($folder)
  291.         }
  292.     }
  293. }
  294.  
  295. # deactivateHook
  296. proc html::DeactivateHook {name} {
  297.     global html::HPwinPositions
  298.     set winSize [getGeometry]
  299.     # When closing size is {0 0 0 0}
  300.     if {$winSize == {0 0 0 0}} {return}
  301.     set html::HPwinPositions([html::GetHpFolder]) $winSize
  302. }
  303.  
  304. namespace eval Home {}
  305. proc Home::DblClick {from to} {html::HomeReturn}
  306.  
  307. proc Home::OptionTitlebar {} {
  308.     global file::separator
  309.     return [split [html::GetHpFolder] ${file::separator}]
  310. }
  311.  
  312. proc Home::OptionTitlebarSelect {item} {
  313.     global file::separator
  314.     set folders [split [html::GetHpFolder] ${file::separator}]
  315.     if [key::optionPressed] {killWindow}
  316.     html::OpenHPwin [eval file join [lrange $folders 0 [lsearch -exact $folders $item]]]
  317. }
  318.  
  319. foreach __char {a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 . _ -} {
  320.     Bind '$__char' "html::SearchInHPwin $__char" Home
  321. }
  322. unset __char
  323.  
  324. Bind '\r' html::HomeReturn Home
  325. Bind '\r' <o> {html::HomeReturn 1} Home
  326. Bind down <c> html::HomeReturn Home
  327. Bind down <co> {html::HomeReturn 1} Home
  328. Bind enter html::HomeReturn Home
  329. Bind enter {html::HomeReturn 1} Home
  330. Bind down     browse::Down Home
  331. Bind up     browse::Up Home
  332. Bind '\r' <c> html::HpWinBack Home
  333. Bind '\r' <co> {html::HpWinBack 1} Home
  334. Bind enter <c> html::HpWinBack Home
  335. Bind enter <co> {html::HpWinBack 1} Home
  336. Bind up <c> html::HpWinBack Home
  337. Bind up <co> {html::HpWinBack 1} Home
  338. Bind 'r' <c> html::RefreshHpWin Home
  339. Bind 'c' <cs> html::CopyURL Home
  340.  
  341. set Home::Time 0
  342.